#include "Terrain.h"

using namespace std;

CTerrain::CTerrain(const char *mapFile, CSceneManager *smgr)
:m_SceneManager(smgr)
{
  //memset(m_Textures, 0, TERRAIN_TYPE_NUMBER);
  for (int i=0; i<TERRAIN_TYPE_NUMBER; ++i)
  {
    m_Textures[i] = 0;
  }
  loadMap(mapFile);
  resetMap();
}

void CTerrain::loadMap(const char *mapFile)
{
  cout<<"Map LoadIng..."<<endl;
  string str;
  string substr;
  ifstream file(mapFile);
  int i = 0;
  int j = 0;
  while (getline(file, str))
  {
	  string::size_type begin = 0;
	  string::size_type end = 0;
	  begin = str.find_first_not_of(" ",begin);
	  while (begin != string::npos)
	  {
		  end = str.find_first_of(" ",begin);
		  if(end == std::string::npos)
		  {
			  substr = str.substr(begin);
			  m_TerrainMap[i][j] =atoi(substr.c_str());				
			  ++j;
			  break;			
		  }
		  substr = str.substr(begin,end-begin);
		  m_TerrainMap[i][j] = atoi(substr.c_str());	
		  ++j;
		  begin = str.find_first_not_of(" ",end);
	  }
	  j = 0;	
	  ++i;
  }
  cout<<"Texture LoadIng..."<<endl;
  m_Textures[TERRAIN_ROAD] = m_SceneManager->createTexture(ROAD_TEXTURE_PATH.c_str());
  m_Textures[TERRAIN_BRICK] = m_SceneManager->createTexture(BRICK_TEXTURE_PATH.c_str());
  m_Textures[TERRAIN_WATER] = m_SceneManager->createTexture(WATER_TEXTURE_PATH.c_str());
  m_Textures[TERRAIN_STONE] = m_SceneManager->createTexture(STONE_TEXTURE_PATH.c_str());
  m_Textures[TERRAIN_FOREST] = m_SceneManager->createTexture(FOREST_TEXTURE_PATH.c_str());
  m_Textures[TERRAIN_HOME] = m_SceneManager->createTexture(HOME_TEXTURE_PATH.c_str());
  cout<<"Map LoadIng..."<<endl;
  m_BoxMesh = m_SceneManager->createMesh(TERRAIN_BOX_PATH.c_str(),MESH_TYPE_MD2);

}
void CTerrain::resetMap()
{
  CMeshSceneNode *meshSceneNode;
  meshSceneNode = m_SceneManager->addMeshSceneNode(m_BoxMesh,m_SceneManager->getRootNode()
    ,Vector3f(95.0f,-2.5f,0.0f),Vector3f::ZERO,Vector3f(19.0f,0.0f,15.0f));
  meshSceneNode->setTexture(m_Textures[TERRAIN_ROAD]);

  for (int height = 0; height < TERRAIN_HEIGHT; height ++)
  {
    for (int width = 0; width < TERRAIN_WIDTH; width ++)
    {
      switch(m_TerrainMap[height][width])
      {
      case TERRAIN_ROAD:		
        //meshSceneNode = m_SceneManager->addMeshSceneNode(m_BoxMesh, m_SceneManager->getRootNode()
        //  ,Vector3f((/*BOX_SCALE/2*/+BOX_SCALE*width)*BOX_LENGTH,0.0,(BOX_SCALE*height)*BOX_LENGTH),Vector3f::ZERO,Vector3f(BOX_SCALE,/*BOX_SCALE + BOX_SCALE*/0.0,BOX_SCALE));
        //meshSceneNode->setTexture(m_Textures[TERRAIN_ROAD]);
        ////meshSceneNode->setEffect("phong");
        break;
      case TERRAIN_BRICK:
        meshSceneNode = m_SceneManager->addMeshSceneNode(m_BoxMesh, m_SceneManager->getRootNode()
          ,Vector3f((/*BOX_SCALE/2*/+BOX_SCALE*width)*BOX_LENGTH,0.0,(BOX_SCALE*height)*BOX_LENGTH),Vector3f::ZERO,Vector3f(BOX_SCALE,BOX_SCALE + BOX_SCALE,BOX_SCALE));
        meshSceneNode->setTexture(m_Textures[TERRAIN_BRICK]);
        //meshSceneNode->setEffect("phong");
        break;
      case TERRAIN_WATER:
        meshSceneNode = m_SceneManager->addMeshSceneNode(m_BoxMesh, m_SceneManager->getRootNode()
          ,Vector3f((/*BOX_SCALE/2*/+BOX_SCALE*width)*BOX_LENGTH,0.0,(BOX_SCALE*height)*BOX_LENGTH),Vector3f::ZERO,Vector3f(BOX_SCALE,BOX_SCALE + BOX_SCALE,BOX_SCALE));
        meshSceneNode->setTexture(m_Textures[TERRAIN_WATER]);
        meshSceneNode->setEffect("phong");
        break;
      case TERRAIN_FOREST:
        meshSceneNode = m_SceneManager->addMeshSceneNode(m_BoxMesh, m_SceneManager->getRootNode()
          ,Vector3f((/*BOX_SCALE/2*/+BOX_SCALE*width)*BOX_LENGTH,0.0,(BOX_SCALE*height)*BOX_LENGTH),Vector3f::ZERO,Vector3f(BOX_SCALE,BOX_SCALE + BOX_SCALE,BOX_SCALE));
        meshSceneNode->setTexture(m_Textures[TERRAIN_FOREST]);
        //meshSceneNode->setEffect("phong");
        break;
      case TERRAIN_STONE:
        meshSceneNode = m_SceneManager->addMeshSceneNode(m_BoxMesh, m_SceneManager->getRootNode()
          ,Vector3f((/*BOX_SCALE/2*/+BOX_SCALE*width)*BOX_LENGTH,0.0,(BOX_SCALE*height)*BOX_LENGTH),Vector3f::ZERO,Vector3f(BOX_SCALE,BOX_SCALE + BOX_SCALE,BOX_SCALE));
        meshSceneNode->setTexture(m_Textures[TERRAIN_STONE]);
        //meshSceneNode->setEffect("phong");
        break;
      case TERRAIN_HOME:
        meshSceneNode = m_SceneManager->addMeshSceneNode(m_BoxMesh, m_SceneManager->getRootNode()
		  ,Vector3f((/*BOX_SCALE/2*/+BOX_SCALE*width)*BOX_LENGTH,0.0,(BOX_SCALE*height)*BOX_LENGTH),Vector3f::ZERO,Vector3f(BOX_SCALE,BOX_SCALE + BOX_SCALE,BOX_SCALE));
        meshSceneNode->setTexture(m_Textures[TERRAIN_HOME]);
        //meshSceneNode->setEffect("phong");
        break;
      default:
        break;
      }
    }
  }

}